<?php
class ControllerCommunityYourday extends Controller{
    private $error = array();

    public function index(){
        $this->language->load('community/yourday');
        $this->language->load('community/community_header');
        $this->load->model('community/yourday');

        $this->document->setTitle($this->language->get('heading_title'));

        //detect if user login
        if($this->customer->isLogged()){
            $this->data['is_user_login']=TRUE;
        }else{
            $this->data['is_user_login']=FALSE;
        }

        if(($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()){
            $this->model_community_yourday->addName($this->request->post);

            $this->session->data['last_submit_time']=time();

            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->link('community/yourday'));
        }

        $this->data['breadcrumbs']=array();
        
        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('common/home'),
            'text'      =>      $this->language->get('text_home'),
            'separator'=>      FALSE
        );
        
        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('community/community'),
            'text'      =>      $this->language->get('text_community'),
            'separator' =>      $this->language->get('text_separator')
        );

        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('community/yourday'),
            'text'      =>      $this->language->get('heading_title'),
            'separator' =>      $this->language->get('text_separator')
        );

        $this->data['text_ucp']=$this->language->get('text_ucp');

        if($this->customer->getForumUserNotifyPm()==0){
            $this->data['text_new_message']=sprintf($this->language->get('text_new_message'),'0');
        }else{
            $this->data['text_new_message']=sprintf($this->language->get('text_new_message'),$this->customer->getForumUserNewPm());
        }

        $this->data['text_nominate_your_name_tip']=$this->language->get('text_nominate_your_name_tip');
        $this->data['text_no_name_today']=$this->language->get('text_no_name_today');
        $this->data['text_name_tip']=$this->language->get('text_name_tip');
        $this->data['text_no_names']=$this->language->get('text_no_names');
        $this->data['text_date']=$this->language->get('text_date');
        $this->data['text_name']=$this->language->get('text_name');
        $this->data['text_not_started']=$this->language->get('text_not_started');
        $this->data['text_poll_on']=$this->language->get('text_poll_on');
        $this->data['text_claim_freebie']=$this->language->get('text_claim_freebie');
        $this->data['text_ready']=$this->language->get('text_ready');
        $this->data['text_count_down']=$this->language->get('text_count_down');
        $this->data['text_days']=$this->language->get('text_days');
        $this->data['text_hours']=$this->language->get('text_hours');
        $this->data['text_mins']=$this->language->get('text_mins');
        $this->data['text_secs']=$this->language->get('text_secs');

        $this->data['tab_name_list']=$this->language->get('tab_name_list');
        $this->data['tab_rule_faq']=$this->language->get('tab_rule_faq');
        $this->data['tab_how']=$this->language->get('tab_how');
        $this->data['tab_image']=$this->language->get('tab_image');

        $this->data['entry_nominate_your_name']=$this->language->get('entry_nominate_your_name');
        $this->data['entry_name']=$this->language->get('entry_name');
        $this->data['entry_email']=$this->language->get('entry_email');
        $this->data['entry_newsletter']=$this->language->get('entry_newsletter');

        $this->data['button_vote']=$this->language->get('button_vote');

        $this->data['contact_link']=$this->url->link('information/contact');

        // adding facebook's meta tags for Like button start
        $this->document->addFBMeta('og:title', $this->language->get('your_day_social_title'));
        $this->document->addFBMeta('og:url', $this->url->link('community/yourday'));
        $this->document->addFBMeta('fb:admins', 'micbus');
        $this->document->addFBMeta('og:type', 'website');
        $this->document->addFBMeta('og:site_name', $this->config->get('config_name'));
        $this->document->addFBMeta('og:image', DIR_IMAGE . 'others/your_day_thumb.jpg');

        $this->data['your_day_social_title']=$this->language->get('your_day_social_title');
        $this->data['snslink']=$this->url->link('community/yourday');

        if(isset($this->request->post['name'])){
            $this->data['name']=$this->request->post['name'];
        }elseif($this->customer->isLogged() && !isset($this->session->data['success'])){
            $this->data['name']=$this->customer->getFirstName();
        }else{
            $this->data['name']='';
        }

        if(isset($this->request->post['email'])){
            $this->data['email']=$this->request->post['email'];
        }elseif($this->customer->isLogged() && !isset($this->session->data['success'])){
            $this->data['email']=$this->customer->getEmail();
        }else{
            $this->data['email']='';
        }

        if (isset($this->request->post['captcha'])) {
            $this->data['captcha'] = $this->request->post['captcha'];
        } else {
            $this->data['captcha'] = '';
        }

        if (isset($this->error['name'])) {
            $this->data['error_name'] = $this->error['name'];
        } else {
            $this->data['error_name'] = '';
        }

        if (isset($this->error['email'])) {
            $this->data['error_email'] = $this->error['email'];
        } else {
            $this->data['error_email'] = '';
        }

        if (isset($this->error['captcha'])) {
            $this->data['error_captcha'] = $this->error['captcha'];
        } else {
            $this->data['error_captcha'] = '';
        }

        if (isset($this->error['nominate'])) {
            $this->data['error_nominate'] = $this->error['nominate'];
        } else {
            $this->data['error_nominate'] = '';
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        $this->data['upcoming_names']=array();

        $results=$this->model_community_yourday->getUpcomingNames();

        foreach($results as $result){
            $start_time=$result['upcoming_date']+(60*60*24)-time();
            $days=floor($start_time/(60*60*24))>0 ? floor($start_time/(60*60*24)) : 0;
            $hours=floor(($start_time-$days*(60*60*24))/(60*60))>0 ? floor(($start_time-$days*(60*60*24))/(60*60)) : 0;
            $mins=floor(($start_time-$days*(60*60*24)-$hours*(60*60))/60)>0 ? floor(($start_time-$days*(60*60*24)-$hours*(60*60))/60) : 0;
            $secs=floor($start_time-$days*(60*60*24)-$hours*(60*60)-$mins*60)>0 ? floor($start_time-$days*(60*60*24)-$hours*(60*60)-$mins*60) : 0;

            $this->data['upcoming_names'][]=array(
                'name'              =>      ucwords($result['name']),
                'upcoming_date'     =>      date('l, F jS, Y',$result['upcoming_date']),
                'days'              =>      $days,
                'hours'             =>      $hours,
                'mins'              =>      $mins,
                'secs'              =>      $secs,
                'started'           =>      ($result['being_used'] ? ($result['being_polled'] ? 'polled' : 'going') : 'not_started'),
                'link'              =>      $this->url->link('community/viewtopic','fid=' . $result['forum_id'] . '&tid=' . $result['topic_id'])
            );
        }

        if(file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/community/yourday.tpl')){
            $this->template=$this->config->get('config_template') . '/template/community/yourday.tpl';
		}else{
            $this->template='default/template/community/yourday.tpl';
		}	
			
        $this->children = array(
            'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header',
			'community/community_header'
		);
		
		$this->response->setOutput($this->render()); 	
  	}

    public function captcha() {
        $this->load->library('captcha');

        $captcha = new Captcha();

        $this->session->data['captcha'] = $captcha->getCode();

        $captcha->showImage();
    }

    private function validateForm(){
        $result=$this->model_community_yourday->validateName($this->request->post['name']);

        if($this->request->post['name']==''){
            $this->error['name']=$this->language->get('empty_name');
        }

        if (preg_match('/[\~\!\@\#\$\%\^\&\*\(\)\;\:\'\"\/\?\>\<\,\.\[\]\{\}\-\_\+\=0-9]/i', $this->request->post['name'])) {
            $this->error['name'] = $this->language->get('error_name');
        }

        if(!$result){
            $this->error['name']=$this->language->get('ip_name_nominated');
        }

        if($this->request->post['email']==''){
            $this->error['email']=$this->language->get('empty_email');
        }

        if (!preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['email'])) {
            $this->error['email'] = $this->language->get('error_email');
        }

        if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
            $this->error['captcha'] = $this->language->get('error_captcha');
        }

        if(isset($this->session->data['last_submit_time']) && time()-$this->session->data['last_submit_time']<=10){
            $this->error['nominate'] = $this->language->get('error_nominate_time_span');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }
}
?>